#!/bin/sh

PROG=`basename $0`
JSMDIR=/opt/jrun/jsm-cotacoes

# make jsmdir absolute
case $JSMDIR in
/*) ;; # already absolute
*) JSMDIR=`pwd`/"$JSMDIR" ;; # make full if relative
esac

START=com.livesoftware.jrun.service.ServiceManager
CONTROL=com.livesoftware.jrun.service.ServiceManagerControl
ADMIN=com.livesoftware.jrunadmin.JRunAdminApp

LAUNCHER="$JSMDIR/../bin/launcher.sh -d $JSMDIR $JSMDIR/properties/jsm.properties"
 
#ulimit -n `ulimit -nH` || echo ulimit failed
ulimit -n `ulimit -n` || echo ulimit failed

# See how we were called.
case "$1" in
  start)

	$0 status > /dev/null && { 
	    echo "Another JRun is already running." && exit 1 
	}

	echo "Starting jrun: "

	$LAUNCHER $START "$JSMDIR" &

	sleep 1
	$0 status > /dev/null || {
	    sleep 2
	    $0 status > /dev/null || {
		sleep 4
		$0 status > /dev/null || echo "Error: JRun: Could not start."
	    }
	}

	;;
  stop)
	echo "Shutting down JRun: "
	$LAUNCHER $CONTROL -props "$JSMDIR"/properties -command shutdown

	$0 status > /dev/null && {
	    sleep 1
	    $0 status > /dev/null && echo "Error: JRun doesn't seem to be stopping, try killing it manually."
        }

	;;
  status)
	echo "Checking status of JRun: "
	status=`$LAUNCHER $CONTROL -props "$JSMDIR"/properties -command ping`
	if [ ${status}x = 1x ]; then
	    echo $JSMDIR is running
	    exit 0
        else
	    echo $JSMDIR is stopped
	    exit 1
	fi
	echo
	;;
  restart)
	$0 stop
	$0 start
	;;
  admin)
	echo "Launching JRun Administrator"
	$LAUNCHER $ADMIN "JRunAdminstrator" -root "$JSMDIR" -prop properties
	;;
  *)
	echo "Usage: $0 {start|stop|restart|admin|status}"
	exit 1
esac


